package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;

@RestController
@RequestMapping("admin/product/")
public class FileUploadController {
    @Value("${fileServer.url}")
    private String fileServer;//fileServer=http://192.168.200.128:8080/

    @PostMapping("fileUplode")
    public Result fileUpload(MultipartFile file) throws Exception {
        String configFile = this.getClass().getResource("tracker.conf").getFile();
        String path="";
        if (configFile!=null){
            ClientGlobal.init(configFile);
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageClient1 storageClient1 = new StorageClient1(trackerServer,null);
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());
            path = storageClient1.upload_appender_file1(file.getBytes(), extension, null);
        }
        return Result.ok(fileServer+path);
    }
}
